// pages/insertHouse/insertHouse.js
var app = getApp();
var liunxUrl=app.globalData.liunxUrl
var localUrl=app.globalData.localUrl
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date:"",
    Recording:{
      userId:"",
      money:"",
      type:"",
      updateTime:"",
      year:"",
      description:""
    },
   types:[
    {
      typeName:"收入",
      id:0
    },
    {
      typeName:"支出",
      id:1
    },
    {
      typeName:"投资",
      id:2
    },
    {
      typeName:"还款",
      id:3
    },
    {
      typeName:"购物",
      id:4
    },
    {
      typeName:"房租",
      id:5
    },
   ],
   typeIndex:"",
   type:"",
  },

  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },


  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let region_ = e.detail.value[1]+e.detail.value[2]
    this.setData({
      region: region_
    })
  },
  
  bindStart:function(e){
    this.data.typeIndex = e.detail.value;
    this.data.type = this.data.types[this.data.typeIndex];
    this.setData({
      "type":this.data.types[this.data.typeIndex]
    })
    console.info(this.data.type)
  },


  fanHui(){
    wx.navigateBack({
      complete: (res) => {},
    })
  }
,
  // 添加的方法
  formSubmit(e){
    console.info(e.detail.value)
    this.setData({
      user:app.globalData.user
    })
    let  oldYear = e.detail.value.updateTime;

    this.data.Recording.userId = app.globalData.user.id;
    this.data.Recording.money = e.detail.value.money;
    this.data.Recording.type = this.data.type.id;
    this.data.Recording.updateTime = e.detail.value.updateTime;
    this.data.Recording.year = oldYear.substring(0,oldYear.indexOf('-'))
    this.data.Recording.description = e.detail.value.description;
wx.request({
  url: liunxUrl+'/rec/save', 
  method:'POST',
  data:{
    ...this.data.Recording
  },
  success(res) {
    console.info(res.data.data);
    if(res.data.code==200){
      wx.navigateBack({
        complete: (res) => {},
      })
    }else{
      wx.showToast({
          title: '添加失败',
        // icon:'error',
          image:"/pages/image/jg.png",
         duration:2000
     })
    }
   
  }
})

},



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    if(app.globalData.user==null){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})